<template>
  <div class="app-container">
    <div class="flex justify-between">
      <div>
          <el-button type="primary" icon="el-icon-plus" size="default" @click="addNewProfile">新增配置</el-button>
      </div>
      <div class="flex flex-wrap">
        <div>
          <span>状态：</span>
          <el-select v-model.number="listQuery.is_open"
                      class="filter-item marginRB10"
                      filterable
                      placeholder="请选择状态"
                      style="width: 200px"
          >
            <el-option
              v-for="item in selectList"
              :key="item.key"
              :label="item.lab"
              :value="item.key"
            ></el-option>
          </el-select>
        </div>
        <div>
          <span>内容：</span>
          <el-input
            v-model.number="listQuery.content"
            class="marginRB10"
            clearable
            placeholder="请输入内容"
            style="width: 200px"
          ></el-input>
        </div>
        <el-button
          class="marginRB10"
          icon="el-icon-search"
          type="primary"
          @click="getList"
        >搜索
        </el-button>
        <el-button class="marginRB10" @click="resetSubmit">重置</el-button>
      </div>
    </div>

    <el-table
      ref="dataList"
      :data="menuList"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      border
      row-key="id"
      style="margin-bottom: 20px"
    >
      <el-table-column :index="indexMethod" type="index" width="150" label="编号"></el-table-column>
      <el-table-column label="发布内容" prop="content"></el-table-column>
      <el-table-column label="创建时间" prop="created_at" sortable width="250"></el-table-column>
      <el-table-column label="状态" prop="is_open" width="250">
        <template slot-scope="scope">
          <span v-if="scope.row.is_open === 1" class="text-green-600">启用</span>
          <span v-if="scope.row.is_open === 0" class="text-red-500">禁止</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(row)"
            style="margin-right: 10px;"
          >编辑
          </el-button>
          <el-popconfirm
              cancel-button-text="取消"
              confirm-button-text="删除"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除此条记录吗？"
              @confirm="handleDelete(row)"
            >
              <el-button size="default" slot="reference" type="text" style="color: #f56c6c;">删除</el-button>
            </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <cus-pagination
      v-show="total > 0"
      :limit.sync="listQuery.perPage"
      :page.sync="listQuery.page"
      :total="total"
      @pagination="getList"
    />

    <!-- 新增/编辑 弹窗 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      center
      width="30%"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="发布内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入发布内容"/>
        </el-form-item>
        <el-form-item label="选择状态" prop="is_open">
            <el-radio-group v-model="form.is_open" @change="onChange">
                <el-radio v-for="item in checkedItems" :key="item.key" :label="item.key">
                    {{item.lab}}
                </el-radio>
            </el-radio-group>
            
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listData, saveData, deleteMessage } from '@/api/operatingConfiguration/newUserMessage'

export default {
  data() {
    return {
      confirmBtn: false,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10
      },
      selectList: [
        { key: undefined, lab: '全部状态' },
        { key: 1, lab: '启用 ' },
        { key: 0, lab: '禁止' }
      ],
      checkedItems: [
        { key: 1, lab: '启用 ' },
        { key: 0, lab: '禁止' }
      ],
      menuList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '增加'
      },
      form: {
        content: undefined,
        is_open: undefined
      },
      rules: {
        content: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
        is_open: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    indexMethod(index) {
      return (this.listQuery.page - 1) * this.listQuery.perPage + index + 1
    },
    getList() {
      this.listLoading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
      }
      listData(this.listQuery).then(({ data }) => {
        this.menuList = data.list
        this.total = data.meta.total
        this.listLoading = false
        this.confirmBtn = false
        this.totalData = [data.header]
      })
    },
    handleDetail(row) {
      this.dialogFormVisible = true
      this.dialogStatus = 'update'
      this.form = JSON.parse(JSON.stringify(row))
    },
    submitForm() {
      if (this.confirmBtn) return
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          // let formData = new FormData()
          // for (let item in this.form) {
          //   this.form[item] === undefined
          //     ? ''
          //     : formData.append(item, this.form[item])
          // }
          let method = ''
          let formData = {}
          if(this.form.id) {
            method = 'put'
            formData.id = this.form.id
          }else {
            method = 'post'
            formData.id = ''
          }
          formData.content = this.form.content
          formData.is_open = this.form.is_open
          saveData(formData, method).then((response) => {
            if (response.code === 200) {
              this.getList()
              // this.$notify({
              //   type: 'success',
              //   message: '操作成功',
              //   position: 'bottom-right',
              //   showClose: false,
              //   duration: 3000
              // })
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        }
      })
    },
    resetForm() {
      this.form = {
        name: undefined
      }
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    },
    resetSubmit() {
      this.listQuery = {
        page: 1,
        perPage: 10
      }
      this.getList()
    },
    addNewProfile() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
    },

    /* 提交 */
    // async handleSubmit() {
    //   if (this.local_user_code.trim() === '') {
    //     return this.$message.warning('请输入 用户 code')
    //   }
    //   const { code, message } = await toWithDrawBlock(this.local_user_code)
    //   if (code === 200) {
    //     this.$message.success('添加成功')
    //     this.visible = false
    //     this.local_user_code = ''
    //     await this.initData()
    //   } else {
    //     this.$message.error(message || '添加失败')
    //   }
    // },
    // 删除
    async handleDelete(row) {
      const { id } = row
      const { code } = await deleteMessage(id)
      if (code === 200) {
        this.$message.info('操作成功')
        await this.getList()
      }
    },
    onChange() {}
  }
}
</script>
